home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6968 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  71 lines

  1. Path: news.telsib.ru!voyager
  2. From: a-kovalenko@star.nmb.ru (Alexey Kovalenko)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C calling C++ function !!!
  5. Date: Wed, 21 Feb 96 12:24:45 GMT
  6. Organization: NMB
  7. Message-ID: <4gf2ud$oo0_001@news.telsib.ru>
  8. NNTP-Posting-Host: voyager.nmb.ru
  9. X-Newsreader: News Xpress Version 1.0 Beta #4
  10.  
  11. Hello, all.
  12.  
  13. Few days ago Norm Bryar <normanb@halcyon.com> in his answer has written:
  14.  
  15. >Something tells me it's not that simple.  
  16. >
  17. [Skipped]
  18. >I believe the accepted thing to do is define a set of 'C' wrapper APIs
  19. >that you can export to your 'C' modules.  Internally, your wrappers do
  20. >class method calls.  
  21.  
  22. It's wrong. Function in C++, defined as "C", just uses predefined C calling 
  23. convention instead compiler's preferred.
  24.  
  25. Real solution here (don't say "It will not work" - I'm using this ):
  26.  
  27. Situation : I'm working on project that contain both C and C++ sources and I 
  28. don't want to make C sources C++. I'm needing to call function-classmember 
  29. from C sources . Function must return char* by index(access to indexed strings 
  30. array, fuction in C used instead operator[] in C++). 
  31.  
  32. header contains:
  33.  typedef char * (*MsgFunc)(int iNo );
  34.  
  35. C++ file contains:
  36.  
  37.  #include "header.h"
  38.  #include "cassdef.hpp"
  39.  
  40.  static Message * _Msg = new Message("messages.msg");
  41.  static Message & Msg1 = *_Msg;
  42.   // Two variables to avoid compiler warning about
  43.  
  44.  char * Msg( int iNo )
  45.  {
  46.    return Msg1[iNo];        // operator []
  47.  }
  48.  
  49.  MsgFunc Msg = Msg1;
  50.  
  51. And C file contains:
  52.  
  53.  #include "header.h"
  54.  extern MsgFun Msg;
  55.  {
  56.   printf( Msg(3) );
  57.  }
  58.  
  59. So, I'm calling C++ opertaor and class initialises before the "main" function.
  60.  
  61. =============================================================
  62. Alexy Kovalenko
  63. Siberian State Academy of Telecommunications and Informatics
  64.  
  65. Internet:
  66. a-kovalenko@star.nmb.ru
  67. a-kovalenko@voyager.nmb.ru
  68. koval@cad.nsk.su
  69. =============================================================
  70. // To love and to weep of love not for shame.
  71.